'use strict';

import { addEvent } from "../../lib/tools.js";
import { save, update, delData, uploadFile } from "./interface.js";

const check = {
    // 添加 class 类名
    addClass(elem, className) {
        if (!elem || !className) return;
        elem.classList.add('has-error');
    },

    // 移除 class 类名
    removeClass(elem, className) {
        if (!elem || !className) return;
        elem.classList.remove('has-error');
    },

    isEmpty(elem, value, msg = '必填项不能为空') {
        var parentElem = elem.parentNode,
            nextElem = elem.nextElementSibling;

        if (value === '' || value == null) {
            elem.focus();
            layer.msg(msg, {
                icon: 2,
            });
            this.addClass(parentElem, 'has-error');
            return true;
        }else{
            this.removeClass(parentElem, 'has-error');
        }
    },
}

// 成功回调
const success = function (result) {
    if (result.code === 0) {
        layer.msg(result.msg, {
            icon: 1,
            time: 1,
        }, function () {
            if (result.data) {
                window.location.href = result.data;
            }else{
                location.reload();
            }
        });
    } else if (result.code === 1) {
        layer.msg(result.msg, {
            icon: 2,
        });
    } else {
        layer.msg(result.msg, {
            icon: 2,
        });
    }
}
// 错误回调
const error = function (result) {
    layer.msg(result, {
        icon: 2,
    });
}

// form
export const http = {
    // form submit
    formSubmit(elem) {
        addEvent(elem, 'submit', function () {
            var inputs = this.getElementsByTagName('input'),
                textarea = this.getElementsByTagName('textarea')[0],
                selects = this.getElementsByTagName('select');

            // input
            for (var i = 0; i < inputs.length; i++) {
                // 忽略不验证的input
                if (inputs[i].readOnly) continue;
                if (inputs[i].getAttribute("type") === "file") continue;
                if (inputs[i].getAttribute("type") === "hidden") continue;
                if (inputs[i].getAttribute("type") === "radio") continue;

                var classList = [].slice.call(inputs[i].classList, 0);
                if ( (classList.indexOf('ignore') > -1) ) continue;

                // 检验是否为空
                if (check.isEmpty(inputs[i], inputs[i].value)) return;
            }

            var data = new FormData(this),
                url = this.getAttribute('ajax-url');

            if (data.get('id')) {
                return update(url, data, success, error);
            }
            return save(url, data, success, error);
        })
    },

    // del
    del(elem) {
        for (var i = 0; i < elem.length; i++) {
            (function (i) {
                addEvent(elem[i], 'click', function () {
                    var id = this.getAttribute('ajax-id'),
                        url = this.getAttribute('ajax-url');
                    delData(url, {id: id}, success, error);
                });
            }(i));
        }
    },

    // update data
    updateData(url, form_data) {
        update(url, form_data, success, error);
    },
}
